<template>
  <el-dialog :title="!dataForm.id ? '创建员工' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <!-- <el-form-item label="头像" prop="pic">
        <el-button
          v-if="dataForm.pic == '' || dataForm.pic == null"
          type="primary"
          @click="uploadHandle()"
          >上传</el-button
        >
        <img
          class="Avatars"
          v-if="dataForm.pic"
          :src="dataForm.pic"
          alt=""
          @click="uploadHandle()"
        />
        <p class="ae">建议尺寸：60*60，，图片小于5M</p>
      </el-form-item> -->
      <el-form-item label="账号" prop="account" label-width="85px">
        <el-input v-if="!dataForm.id" v-model="dataForm.account" placeholder="请输入账号"></el-input>
        <span v-else>{{ dataForm.account }}</span>
      </el-form-item>
      <!-- <el-form-item label="世界波账号" prop="sjbAccount" label-width="85px">
        <el-input
          v-model="dataForm.sjbAccount"
          placeholder="请输入世界波账号"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="登录密码" prop="password" v-if="!this.dataForm.id" label-width="85px">
        <el-input v-model="dataForm.password" placeholder="请输入登录密码"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名" prop="name" label-width="85px">
        <el-input v-model="dataForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="tel" label-width="85px">
        <el-input v-model="dataForm.tel" placeholder="请输入联系方式"></el-input>
      </el-form-item>
      <el-form-item label="联系地址" prop="introduction" label-width="85px">
        <el-input v-model="dataForm.introduction" placeholder="请输入联系地址"></el-input>
      </el-form-item>
      <el-form-item label="员工职位" prop="roleId" label-width="85px">
        <el-select v-model="dataForm.roleId" placeholder="请选择职位">
          <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
    <upload v-if="uploadVisible" ref="upload" :uploadList='uploadList'></upload>
  </el-dialog>
</template>

<script>
import md5 from "js-md5";
import Upload from "./oss-upload";
export default {
  data() {
    return {
      isdisabled: false,
      uploadVisible: false,
      visible: false,
      dataForm: {
        roleId: null,
        pic: "",
        jobId: null,
        tel: "",
        password: "",
        rePassword: "",
        name: "",
        account: "",
        sjbAccount: '',
        introduction: "",
        email: "",
      },
      roleList: [],
      jobList: [],
      dataRule: {
        account: [
          {
            required: true,
            message: "成员帐号不能为空/成员帐号已存在",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "成员姓名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        roleId: [
          { required: true, message: "员工角色不能为空", trigger: "blur" },
        ],
        tel: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    Upload
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.isdisabled = true;
          this.$http({
            url: this.$http.adornUrl(
              `/admin/sys/user/view?userId=${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === "200") {
              this.dataForm.account = data.data.account;
              if (data.data.roles[0]) {
                this.dataForm.roleId = data.data.roles[0].roleId;
              }
              this.dataForm.sjbAccount = data.data.sjbAccount;
              this.dataForm.tel = data.data.phoneNumber;
              this.dataForm.name = data.data.name;
              // this.dataForm.password = data.data.password;
              // this.dataForm.rePassword = data.data.password;
              this.dataForm.introduction = data.data.address;
              //        roleId: null,
              // pic: "",
              // jobId:null,
              // tel: "",
              // password: "",
              // rePassword: "",
              // name: "",
              // account: "",
              // introduction: "",
              // email: "",
            }
            this.isdisabled = false;
          });
        }
      });
    },
    getJobList() {
      // this.$http({
      //   url: this.$http.adornUrl(`/admin/user/jobList`),
      //   method: "get",
      //   params: this.$http.adornParams(),
      // }).then(({ data }) => {
      //   if (data && data.code === "200") {
      //     this.jobList = data.list;
      //   }
      // });
    },
    getRoleList() {
      this.$http({
        url: this.$http.adornUrl(`/common/role/list`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === "200") {
          this.roleList = data.list;
        }
      });
    },
    uploadHandle() {
      this.uploadVisible = true;
      this.$nextTick(() => {
        this.$refs.upload.init();
      });
    },
    uploadList(e) {
      this.dataForm.pic = e.trim();
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          var userId = {}
          var password = {}
          if (this.dataForm.id) {
            userId.userId = Number(this.dataForm.id)
          }
          if (this.dataForm.password) {
            password.password = md5(this.dataForm.password).toLowerCase()
          }
          this.$http({
            url: this.$http.adornUrl(this.dataForm.id ? '/admin/sys/user/update' : `/admin/sys/user/add`),
            method: "post",
            data: this.$http.adornData({
              account: this.dataForm.account,
              address: this.dataForm.introduction,
              name: this.dataForm.name,
              roleIdList: [this.dataForm.roleId],
              sjbAccount: this.dataForm.sjbAccount,
              phoneNumber: this.dataForm.tel,
              ...userId,
              ...password
            }),
          }).then(({ data }) => {
            if (data && data.code === "200") {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
<style >
.ae {
  color: red;
  font-size: 12px;
  line-height: 15px;
  margin: 0;
  margin-top: 10px;
}

.Avatars {
  width: 40px;
  height: 40px;
  border: 1px solid #ccc;
}

.v-modal {
  z-index: 1999 !important;
}
</style>